package com.klhappy.houyuan.activity;

import com.klhappy.houyuan.utils.DataCleanManager;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.AboutVerBean;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.AboutYardModle;
import com.klhappy.houyuan.utils.JsonCode;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class About_HouyuanActivity extends AbsActivity implements CallBackInterModle {

	@InjectView(R.id.title)
	TextView title;
	@InjectView(R.id.ver_tv)
	TextView ver_tv;
	@InjectView(R.id.regist_code)
	TextView regist_code;

	@OnClick(R.id.click_ra)
	public void click_ra(){
		finish();
	}

	@OnClick(R.id.feed_back)
	public void feed_back(){
		startActivity(new Intent(this, FeedBackActivity.class));
	}

	@OnClick(R.id.go_score)
	public void go_score(){
		Uri uri = Uri.parse("market://details?id=" + this.getPackageName());
		Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
		try {
			startActivity(goToMarket);
		} catch (ActivityNotFoundException e) {
			Toast.makeText(this, "亲，请下载应用商店！", Toast.LENGTH_SHORT).show();
		}
	}

	private AboutYardModle yardModle;
	private AboutVerBean verBean;
	private boolean flag = false;
	private String  size;
	private File file;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about_houyuan);
		ButterKnife.inject(this);
		title.setText(R.string.about_houyuan);

		yardModle = new AboutYardModle(this);
		yardModle.setCallBackInterModle(this);
		flag = true;
		showDialog("请稍等...");
		yardModle.getUpdate(1, KeyConstant.verson);
		CleanCacheSize();
	}

	//获取缓存大小
	private void CleanCacheSize(){
		try {
			File file_sdcard = Environment.getExternalStorageDirectory();
			file = new File(file_sdcard, KeyConstant.APP_DIR
					+ File.separator + KeyConstant.IMAGE_DIR + File.separator
					+ KeyConstant.HEAD_SHOT);
			size = DataCleanManager.getSize(file);
			regist_code.setText(size);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@OnClick(R.id.about_ver)
	public void about_ver(){
		yardModle.getUpdate(1, KeyConstant.verson);
	}
	@OnClick(R.id.quit)
	public void quit(){
		startActivity(new Intent(About_HouyuanActivity.this, LoginActivity.class));
//		yardModle.getQuit();
	}
	@OnClick(R.id.clean)
	public void clean(){
		try {
			//清除缓存
			DataCleanManager.clean(file);
			CleanCacheSize();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void CallBackFromModle(String url, BaseJsonBean o) {
		if (url.equals(UrlConstant.VER_URL)){
			if (o.getCode() == 200){
				verBean = JsonCode.getClass(o.getEntity() ,AboutVerBean.class);
				if(flag){
					ver_tv.setText("V" + verBean.getVer_no());
					flag = false;
				}else {
					if (verBean.getVer_no().equals(KeyConstant.verson)){
						showToast("本版本已是最新版本.");
					} else{
						showToast("请到各应用商城下载最新版本!");
					}
				}
			}
			closeDialog();
		}else if(url.equals(UrlConstant.LOGOUT_URL)){
			if(o.getCode() == 200){
				showToast(o.getMsg());
				startActivity(new Intent(About_HouyuanActivity.this , LoginActivity.class));
				finish();
			}else{
				showToast(o.getMsg());
			}
			closeDialog();
		}
	}
}
